package com.wtf.dsp.admin.core.parser;

import com.wtf.dsp.admin.config.CustomMybatisConfiguration;
import com.wtf.dsp.core.constant.NsConstant;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.builder.xml.XMLMapperBuilder;
import org.apache.ibatis.executor.ErrorContext;
import org.springframework.core.io.ByteArrayResource;
import org.springframework.core.io.Resource;

import java.io.InputStream;

/**
 * ClassName: MapperXmlResourceParser
 * Description:
 * date: 2024/9/5 10:11
 *
 * @author linhuanyou
 * @version 1.0
 */
@Slf4j
public class MapperXmlResourceParser {

    /**
     * 加载Mapper的xml资源
     * @param mapperLocation
     * @param targetConfiguration
     */
    public static void loadMapperResource(Resource mapperLocation, CustomMybatisConfiguration targetConfiguration) {
        try {
            XMLMapperBuilder xmlMapperBuilder = new XMLMapperBuilder(mapperLocation.getInputStream(), targetConfiguration, mapperLocation.toString(), targetConfiguration.getSqlFragments());
            xmlMapperBuilder.parse();
            log.info("Success to parse mapping resouce : {}", mapperLocation.toString());
        } catch (Exception var19) {
            throw new RuntimeException("Failed to parse mapping resource: '" + mapperLocation + "'", var19);
        } finally {
            ErrorContext.instance().reset();
        }
    }

    public static void loadMapperResource(String resource, InputStream inputStream, CustomMybatisConfiguration targetConfiguration) {
        try {
            XMLMapperBuilder xmlMapperBuilder = new XMLMapperBuilder(inputStream, targetConfiguration, resource, targetConfiguration.getSqlFragments());
            xmlMapperBuilder.parse();
            log.info("Success to parse mapping resouce : {}", resource);
        } catch (Exception var19) {
            throw new RuntimeException("Failed to parse mapping resource: '" + resource + "'", var19);
        } finally {
            ErrorContext.instance().reset();
        }
    }
}
